package org.wk.core.web.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.cage.Cage;
import com.github.cage.IGenerator;
import com.github.cage.image.Painter;
import com.github.cage.token.RandomTokenGenerator;

/**
* @ClassName	: DefaultAction
* @Package		：com.lbs.core.action
* @Author mars mars@labsport.cn
* @Create Time	：2016年9月19日 下午9:57:13
* @Description	: 默认处理
*/ 
@Controller
public class DefaultAction {
	@Autowired
	private HttpSession httpSession;
	/** 
	* @Title: catche 
	* @Description: 设置验证码
	* @return byte[]
	* @throws 
	*/ 
	@RequestMapping(value="/checkCode",method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
	@ResponseBody
	public byte[] catche(@RequestParam(required=false,name="w")Integer w,@RequestParam(required=false,name="h")Integer h) throws IOException{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		Random rnd = new Random();
		Painter painter = null;
		if(!StringUtils.isEmpty(w)&&!StringUtils.isEmpty(h)){
			painter = new Painter(w,h,null,null,null,rnd);
		}
		IGenerator<String> tokenGenerator = new RandomTokenGenerator(rnd,5);
		Cage cage = new Cage(painter, null,null,null,null, tokenGenerator, rnd);
		String code = cage.getTokenGenerator().next();
		cage.draw(code, bos);
		httpSession.setAttribute("checkCode", code);
		return bos.toByteArray();
	}
}
